package cn.itrip.auth.service;

import cn.itrip.beans.pojo.ItripUser;
import cn.itrip.common.MD5;
import cn.itrip.common.RedisAPI;
import cn.itrip.dao.user.ItripUserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("userService")
public class UserServiceimpl implements UserService {
    @Resource
    private ItripUserMapper itripUserMapper;
    @Resource
    private RedisAPI redisAPI;
    @Resource
    private SmsService smsService;
    @Resource
    private  MailService mailService;
    @Override
    public ItripUser findUserByUserCode(String userCode) throws Exception {
        Map<String, Object> param=new HashMap<String, Object>();
        param.put("userCode",userCode);
        List<ItripUser> user = itripUserMapper.getItripUserListByMap(param);
        if(user.size()>0){
            return user.get(0);
        }else{
            return null;
        }
    }


    @Override
    public boolean activate(String mail, String code) throws Exception {
        String value = redisAPI.get("activation:" +mail);
        if(value.equals(code)){
            ItripUser user = this.findUserByUserCode(mail);
            if (user!=null){
                //进行更新
                user.setActivated(1);
                user.setFlatID(user.getId());
                user.setUserType(0);
                itripUserMapper.updateItripUser(user);
                return true;
            }
        }
        return false;
    }


    public void itriptxCreatUserByPhone(ItripUser user) throws Exception {
        //创建用户
        itripUserMapper.insertItripUser(user);
        //生成验证码
        int code = MD5.getRandomCode();
        //发送验证码(设置模板为1  后面一是指 设置验证码的有效时间)
        smsService.send(user.getUserCode(), "1", new String[]{String.valueOf(code), "5"});
        //缓存验证码到Redis
        redisAPI.set("activation:" + user.getUserCode(), 60*5, String.valueOf(code));
    }

    @Override
    public boolean validatePhone(String phoneNum, String code) throws Exception {
        //1.比对验证码
        String key = "activation:" + phoneNum;
        String value = redisAPI.get(key);
        if (null != value && value.equals(code)) {
            ItripUser user = this.findUserByUserCode(phoneNum);
            if (user!=null){
                //进行更新
                user.setActivated(1);
                user.setFlatID(user.getId());
                user.setUserType(0);
                itripUserMapper.updateItripUser(user);
                return true;
            }
        }
        return false;
    }

    @Override
    public void itriptxCreateUser(ItripUser user) throws Exception {
      //添加用户信息
        itripUserMapper.insertItripUser(user);
        //生成激活码
        String ativationCode = MD5.getMd5(new Date().toLocaleString(),32);
        // 发送邮件
        mailService.sendActivationMail(user.getUserCode(),ativationCode);

        // 将激活码存入 redis
        redisAPI.set("activation:" +user.getUserCode(),30*60,ativationCode);



    }

}
